package com.yun.algorithmproblem.leetcode;

import java.util.ArrayList;
import java.util.List;

public class Leetcode2610 {
    public static void main(String[] args) {
        Leetcode2610 obj=new Leetcode2610();
        int[] nums={1,2,3,4};
        List<List<Integer>> ans = obj.findMatrix(nums);
        for (List<Integer> list : ans) {
            for (Integer integer : list) {
                System.out.print(integer+" ");
            }
            System.out.println();
        }
    }

    public List<List<Integer>> findMatrix(int[] nums) {
        List<List<Integer>> matrix=new ArrayList<>();
        int[] cnt=new int[nums.length];
        for (int num : nums) {
            int i = cnt[num-1]++;
            if(i<matrix.size()){
                matrix.get(i).add(num);
            }else {
                ArrayList<Integer> arrayList = new ArrayList<>();
                arrayList.add(num);
                matrix.add(arrayList);
            }
        }
        return matrix;
    }
}
